package com.customview.wzq.mdbehavior;

import android.animation.Animator;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 创建时间：2017/7/23
 * 作者：kb_jay
 * 功能描述：
 */

public class Custom2Behavior extends CoordinatorLayout.Behavior<View> {

    private boolean mStatus = true;//标记是否显示

    private int mScrollSize;

    public Custom2Behavior() {
    }

    public Custom2Behavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        //上滑隐藏，下滑显示
        if (dy > 0) {
            if (mScrollSize > 0) {
                mScrollSize += dy;
            } else {
                mScrollSize = dy;
            }
        } else {
            if (mScrollSize > 0) {
                mScrollSize = dy;
            } else {
                mScrollSize += dy;
            }
        }
        Log.d("kb_jay", "mScrollSize::" + mScrollSize);
        if (mScrollSize > 40) {
            hide(child);
        } else if (mScrollSize < 0) {
            show(child);
        } else {
            // do nothing
        }
    }

    private void hide(View child) {
        if(mStatus==true){
            child.animate().setDuration(300).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }
                @Override
                public void onAnimationEnd(Animator animation) {
                    mStatus=false;
                }
                @Override
                public void onAnimationCancel(Animator animation) {
                }
                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            }).translationY(child.getHeight());

        }else{
            // do nothing
        }
    }

    private void show(View child) {
        if(mStatus==false){
            child.animate().setDuration(300).setListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }
                @Override
                public void onAnimationEnd(Animator animation) {
                    mStatus=true;
                }
                @Override
                public void onAnimationCancel(Animator animation) {
                }
                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            }).translationY(0);
        }else {
            // do nothing
        }
    }
}
